package 剑指offer;

import java.util.Stack;

/**
 * Created by lenovo】 on 2017/2/20.
 * 定义栈的数据结构，请在该类型中实现一个能够得到栈最小元素的min函数。
 */
public class FindStackMin {

    public Stack<Integer> stack = new Stack<Integer>();
    public Stack<Integer> stackMax = new Stack<Integer>();
    public void push(int node) {
        stack.push(node);
    }

    public void pop() {
        stack.pop();
    }

    public int top() {

        return stack.pop();
    }

    public int min() {

        int size = stack.size();
        int min = stack.pop();
        stackMax.push(min);
        int tmp;
        for (int i = 0; i < size - 1; i++) {
            tmp = stack.pop();
            stackMax.push(tmp);
            if (tmp < min) {
                min = tmp;
            }
        }

        int sizeMax = stackMax.size();
        for (int j = 0; j < sizeMax; j++) {
            stack.push(stackMax.pop());
        }
        return min;
    }
}
